/***************************************************************************
* Lyrics-Player                                                            *
* Copyright (C) 2012 Andrey Derevyanko <derandr@gmail.com>                 *
*                                                                          *
* This program is free software: you can redistribute it and/or modify     *
* it under the terms of the GNU General Public License as published by     *
* the Free Software Foundation, either version 3 of the License, or        *
* (at your option) any later version.                                      *
*                                                                          *
* This program is distributed in the hope that it will be useful,          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of           *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
* GNU General Public License for more details.                             *
*                                                                          *
* You should have received a copy of the GNU General Public License        *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
***************************************************************************/

/*! \file processThread.cpp
 *  \brief Implementation of the thread that creates track descriptions in the form Artist - Song name
 *  \author Andrey Derevyanko <derandr@gmail.com>
 *  \version 2.1
 *  \date 10/10/2012
 *  \copyright GNU Public License
 */

#include "processThread.h"

processThread::processThread()
{
    metaDataMedia = new Phonon::MediaObject;
}

void processThread::setData(QList<trackRecord> *data)
{
    this->m_data = data;
}

void processThread::run()
{
    QObject::connect(metaDataMedia, SIGNAL(metaDataChanged()), &metaDataLoop, SLOT(quit()));
    QObject::connect(&processTimer, SIGNAL(timeout()), SLOT(procTimeElapsed()));
//    QTimer::singleShot(max_proc_time, &metaDataLoop, SLOT(quit()));
    exec();
}

void processThread::metaDataProcess()
{
    for (dataI = 0; dataI < static_cast<unsigned int>(m_data->count()); dataI++)
    {
        if (!(*m_data)[dataI].isProcessed)
        {
            isTimeElapsed = false;
            processTimer.start(max_proc_time);
            metaDataMedia->setCurrentSource(Phonon::MediaSource((*m_data)[dataI].filePath));
            metaDataLoop.exec();
            processTimer.stop();
            QStringList artistStr = metaDataMedia->metaData(Phonon::ArtistMetaData);
            QStringList titleStr = metaDataMedia->metaData(Phonon::TitleMetaData);
            QString metaDataStr;
            if (artistStr.empty() || titleStr.empty() || isTimeElapsed)
            {
                metaDataStr = (*m_data)[dataI].filePath;
                metaDataStr.remove(0, metaDataStr.lastIndexOf('/')+1);
            }
            else
            {
                metaDataStr = artistStr[0] + " - " + titleStr[0];
            }
            (*m_data)[dataI].trackName = metaDataStr;
            (*m_data)[dataI].isProcessed = true;
        }
    }
    emit updateModel();
}

void processThread::procTimeElapsed()
{
    isTimeElapsed = true;
    metaDataLoop.quit();
}
